package com.linkage.jz.bankruptcy.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.LawListInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProManagerListViewAdapter extends BGARecyclerViewAdapter<ManagerUserInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    private boolean operatingGroup = true;

    public void setOperatingGroup(boolean operatingGroup) {
        this.operatingGroup = operatingGroup;
    }

    public ProManagerListViewAdapter(RecyclerView recyclerView) {
        super(recyclerView, R.layout.item_managerlist);
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.iv_manageuser_add);
        viewHolderHelper.setItemChildClickListener(R.id.iv_phone);
        viewHolderHelper.setItemChildClickListener(R.id.iv_email);
        viewHolderHelper.setItemChildClickListener(R.id.ll_manager);

        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ManagerUserInfo userInfo) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        if (userInfo.getCurType() == 1) {
            viewHolderHelper.setVisibility(R.id.test, View.GONE);
            viewHolderHelper.setVisibility(R.id.test2, View.VISIBLE);
//            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
//            viewHolderHelper.setVisibility(R.id.iv_email, userInfo.getContactCreditor() ? View.VISIBLE : View.GONE);
            viewHolderHelper.setVisibility(R.id.iv_phone, userInfo.isHidePhone() ? View.GONE : View.VISIBLE);

            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, userInfo.getGroup().getName().equals("负责人") ? View.GONE : View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, userInfo.getAuth().isDeleteManagementMembers() ? View.VISIBLE : View.GONE);
            if (userInfo.getProjectRole() != null && userInfo.getProjectRole().getValue() == 103) {
                viewHolderHelper.setVisibility(R.id.leader_tip,View.VISIBLE );
            }else{
                viewHolderHelper.setVisibility(R.id.leader_tip,View.GONE);
            }

            if (userInfo.getProjectRole() != null && userInfo.getProjectRole().getValue() == 102) {
                viewHolderHelper.setVisibility(R.id.admin_tip123,View.VISIBLE );
                if(userInfo.getGroupLeader())
                {
                    viewHolderHelper.setVisibility(R.id.leader_tip,View.VISIBLE );
                }else{
                    viewHolderHelper.setVisibility(R.id.leader_tip,View.GONE);
                }

            }else{
                viewHolderHelper.setVisibility(R.id.admin_tip123,View.GONE);
            }

//            viewHolderHelper.setVisibility(R.id.admin_tip, (userInfo.getProjectRole() != null && userInfo.getProjectRole().getValue() == 101) ? View.VISIBLE : View.GONE);
//            viewHolderHelper.setVisibility(R.id.leader_tip, userInfo.getGroupLeader() ? View.VISIBLE : View.GONE);
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getName());
//            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getUser().getLoginname());
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, userInfo.getSubsidiaryOrgan());
            if (userInfo.getUser() != null) {
                ImageView imageView = viewHolderHelper.getImageView(R.id.iv_item_bgaswipe_avatar1);
                Glide.with(imageView.getContext()).load(userInfo.getUser().getAvatar())
                        .bitmapTransform(new CropCircleTransformation(imageView.getContext()))
                        .placeholder(R.drawable.logo)
                        .into(imageView);
            }
        } else {
            viewHolderHelper.setVisibility(R.id.test2, View.GONE);
            viewHolderHelper.setVisibility(R.id.test, View.VISIBLE);
            if (userInfo.getProjectRole() != null && userInfo.getProjectRole().getValue() == 103) {
                viewHolderHelper.setVisibility(R.id.leader_tip,View.VISIBLE );
            }else{
                viewHolderHelper.setVisibility(R.id.leader_tip,View.GONE);
            }

            if (userInfo.getProjectRole() != null && userInfo.getProjectRole().getValue() == 102) {
                viewHolderHelper.setVisibility(R.id.admin_tip123,View.VISIBLE );
                if(userInfo.getGroupLeader())
                {
                    viewHolderHelper.setVisibility(R.id.leader_tip,View.VISIBLE );
                }else{
                    viewHolderHelper.setVisibility(R.id.leader_tip,View.GONE);
                }

            }else{
                viewHolderHelper.setVisibility(R.id.admin_tip123,View.GONE);
            }



//            viewHolderHelper.setVisibility(R.id.leader_tip, userInfo.getGroupLeader() ? View.VISIBLE : View.GONE);
            viewHolderHelper.setVisibility(R.id.iv_manageuser_add, userInfo.getGroup().getName().equals("负责人") ? View.GONE : View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.iv_manageuser_add, userInfo.isAddManagementMembers() ? View.VISIBLE : View.GONE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, userInfo.getGroup().getName().equals("负责人") ? View.GONE : View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, (userInfo.getGroup().getName().equals("负责人") || userInfo.getGroupnum() > 0 )? View.GONE : View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, operatingGroup ? View.VISIBLE : View.GONE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_delete, operatingGroup ? (userInfo.getGroupnum() == 0 ? View.VISIBLE : View.GONE) : View.GONE);
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, userInfo.getGroup().getName() + "（" + userInfo.getGroupnum() + "）");
        }
        swipeItemLayout.setSwipeAble(true);
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}